//
//  FeedbackViewController.m
//  Village
//
//  Created by longrise on 2017/9/15.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "FeedbackViewController.h"
#import "WMTextView.h"
@interface FeedbackViewController ()<UITextViewDelegate>
@property (strong, nonatomic) WMTextView *customTextView;
@property (strong ,nonatomic) UIButton *surebutton;
@end

@implementation FeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"意见反馈";
    self.view.backgroundColor = kBackgroundColor;
    [self setview];
    // Do any additional setup after loading the view.
}


- (void)setview{
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(16*kScreenScaleX, 22*kScreenScaleY, 300*kScreenScaleX, 15*kScreenScaleY)];
    lab.font = [UIFont systemFontOfSize:15*kScreenScaleX];
    lab.text = @"欢迎您提出宝贵的意见和建议。";
    [self.view addSubview:lab];
    
    UILabel *lab1 = [[UILabel alloc] initWithFrame:CGRectMake(16*kScreenScaleX, 46*kScreenScaleY, 344*kScreenScaleX, 50*kScreenScaleY)];
    lab1.text = @"我们将及时与您反馈,而您留下的每个字都将用于改善我们的软件。";
    lab1.numberOfLines = 0;//表示label可以多行显示
    lab1.font = [UIFont systemFontOfSize:15*kScreenScaleX];
    [self.view addSubview:lab1];
    
    self.customTextView = [[WMTextView alloc] init];
    _customTextView.frame = CGRectMake(13*kScreenScaleX, 103*kScreenScaleY, 350*kScreenScaleX, 200*kScreenScaleY);
    _customTextView.delegate = self;
    _customTextView.layer.cornerRadius = 5.0*kScreenScaleX;//2.0是圆角的弧度，根据需求自己更改
    [_customTextView.layer setBorderColor:[Util colorWithHexString:@"cccccc"].CGColor];
    _customTextView.layer.borderWidth = 0.5f*kScreenScaleX;//设置边框颜色

    _customTextView.placehoder = @"在这里输入文字,最多200字";
    _customTextView.placehoderColor = [Util colorWithHexString:@"999999"];
    [self.view addSubview:_customTextView];
    
    
    _surebutton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_surebutton setTitle:@"提交反馈" forState:UIControlStateNormal];
    _surebutton.tintColor = [UIColor whiteColor];
    _surebutton.layer.cornerRadius = 5.0*kScreenScaleX;//2.0是圆角的弧度，根据需求自己更改
    _surebutton.titleLabel.font = [UIFont systemFontOfSize:18*kScreenScaleX];
    _surebutton.frame = CGRectMake(30*kScreenScaleX, 323*kScreenScaleY, 315*kScreenScaleX, 45*kScreenScaleY);
    //    [_surebutton setBackgroundImage:[UIImage imageNamed:@"BG"]forState:UIControlStateNormal];
    _surebutton.backgroundColor = K_Anniuhong;
    [_surebutton addTarget:self action:@selector(sureaction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_surebutton];

}
- (void)sureaction{
    
    if (!_customTextView.text.length){
         [MBProgressHUD showError:@"请输入内容!" toView:self.navigationController.view];
    }else if (_customTextView.text.length > 200){
          [MBProgressHUD showError:@"输入内容多于200字!" toView:self.navigationController.view];
    }else{
        NSDate *date = [self getInternetDate];
        
        NSString *string = [self stringFromDate:date];
        LSLog(@"msg -----  %@",string);
        NSString *str =  [string substringToIndex:20];//截取掉下标7之后的字符串
        NSMutableDictionary *bean = [NSMutableDictionary dictionary];
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        
        [dict setObject:_customTextView.text forKey:@"content"];
        [dict setObject:str forKey:@"backtime"];
        [dict setObject:[Globle getInstance].account.userid forKey:@"userid"];
        [dict setObject:[Globle getInstance].account.account forKey:@"backuser"];
        [bean setValue:dict forKey:@"entitybean"];
        
        [NetRequestClass Post:updateappFeedbackURL Parameters:bean success:^(id result) {
            [MBProgressHUD hideHUDForView:self.navigationController.view];
            [MBProgressHUD showSuccess:result[@"message"] toView:self.navigationController.view Completion:nil];
            [[Globle getInstance] saveAccountInfo:result[@"data"]];
            [self.navigationController popViewControllerAnimated:YES];
            LSLog(@"msg -----  %@",result);
        } failure:^(id valueCode, id error) {
            [MBProgressHUD hideHUDForView:self.navigationController.view];
            [MBProgressHUD showError:error toView:self.navigationController.view];
        }];
        
        NSLog(@"%@",_customTextView.text);
    }
  
}
- (NSString *)stringFromDate:(NSDate *)date
{
    //获取系统当前时间
    NSDate *currentDate = [NSDate date];
    //用于格式化NSDate对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设置格式：zzz表示时区
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
    //NSDate转NSString
    NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
    //输出currentDateString
    NSLog(@"%@",currentDateString);
    return currentDateString;
}
- (NSDate *)getInternetDate
{
    NSString *urlString = @"http://m.baidu.com";
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString: urlString]];
    [request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
    [request setTimeoutInterval: 2];
    [request setHTTPShouldHandleCookies:FALSE];
    [request setHTTPMethod:@"GET"];
    NSHTTPURLResponse *response;
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    
    NSString *date = [[response allHeaderFields] objectForKey:@"Date"];
    date = [date substringFromIndex:5];
    date = [date substringToIndex:[date length]-4];
    NSDateFormatter *dMatter = [[NSDateFormatter alloc] init];
    dMatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [dMatter setDateFormat:@"dd MMM yyyy HH:mm:ss"];
    NSDate *netDate = [[dMatter dateFromString:date] dateByAddingTimeInterval:60*60*8];
    
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: netDate];
    NSDate *localeDate = [netDate  dateByAddingTimeInterval: interval];
    return localeDate;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
